﻿Imports System.Data.OleDb
Public Class frmMucTruHoaDon
    Private Sub frmMucTruHoaDon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            MucTruHoaDon_Load()
            'Phân quyền Form
            Dim aPermission(4) As Boolean
            aPermission = PhanQuyenForm("MenuMucTruHoaDon", frmMain.UserId)
            cmdAddNew.Enabled = aPermission(0)
            cmdEdit.Enabled = aPermission(1)
            cmdDelete.Enabled = aPermission(2)
        Catch ex As Exception

        End Try
    End Sub
    Public Sub MucTruHoaDon_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsLoaiSanPham As New DataSet
            Dim dtLoaiSanPham As New DataTable
            Dim query As String = "Select * From MucTruHoaDon Order By TenMucTru ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsLoaiSanPham)
            dbConn.Close()
            dtLoaiSanPham = dsLoaiSanPham.Tables(0)
            dtLoaiSanPham.Columns.Add("STT")
            dtLoaiSanPham.Columns.Add("TruPhanTram")
            For i As Integer = 0 To dtLoaiSanPham.Rows.Count - 1
                dtLoaiSanPham.Rows(i)("STT") = i + 1
                Dim s As String = dtLoaiSanPham.Rows(i)("TheoPhanTram")
                If dtLoaiSanPham.Rows(i)("TheoPhanTram") = "Yes" Then
                    dtLoaiSanPham.Rows(i)("TruPhanTram") = dtLoaiSanPham.Rows(i)("Tru") & " %"
                Else
                    dtLoaiSanPham.Rows(i)("TruPhanTram") = Format(dtLoaiSanPham.Rows(i)("Tru"), "N2")
                End If
            Next
            dgvMucTruHoaDon.AutoGenerateColumns = False
            dgvMucTruHoaDon.DataSource = dtLoaiSanPham
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub cmdAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNew.Click
        Try
            ShowModalForm(frmThemMucTruHoaDon, False, False)
        Catch ex As Exception
            MessageBox.Show(ex.InnerException.ToString)
        End Try
    End Sub

    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Try
            frmThemMucTruHoaDon.MucTruHoaDonId = Integer.Parse(dgvMucTruHoaDon.Rows(dgvMucTruHoaDon.CurrentCell.RowIndex).Cells("MucTruHoaDonId").Value.ToString)
            ShowModalForm(frmThemMucTruHoaDon, False, False)
            frmThemMucTruHoaDon.MucTruHoaDonId = -1
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Try
            Dim IDLoai As Integer = Integer.Parse(dgvMucTruHoaDon.Rows(dgvMucTruHoaDon.CurrentCell.RowIndex).Cells("MucTruHoaDonId").Value.ToString)
            Dim LoaiSanPham As String = dgvMucTruHoaDon.Rows(dgvMucTruHoaDon.CurrentCell.RowIndex).Cells("TenMucTru").Value.ToString
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Bạn muốn xóa mục trừ : " & LoaiSanPham, _
                  "Xóa mục trừ hóa đơn", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                'Update
                Dim connectAccess As New DataAccess
                Dim dbConn As New OleDb.OleDbConnection
                Dim dbCommand As OleDbCommand
                Dim query As String = "Delete from MucTruHoaDon Where MucTruHoaDonID=" & IDLoai
                connectAccess.ConnectDatabase_DBAccess(dbConn)
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
                MucTruHoaDon_Load()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

End Class